package edu.hadassah.prettyprint.printer.xmlprinter;

import edu.hadassah.prettyprint.doc.xml.Attribute;
import edu.hadassah.prettyprint.doc.xml.Comment;
import edu.hadassah.prettyprint.doc.xml.Tag;
import edu.hadassah.prettyprint.doc.xml.Text;

public class SimpleXMLPrinter implements XMLDocumentPrinter {

	private int depth;
	private final StringBuilder output;

	public SimpleXMLPrinter() {
		super();
		depth = -1;
		output = new StringBuilder();
	}

	public void printStart(Tag tag) {
		depth++;
		if (depth > 0){
			output.append("<br/>");
		}
		addIndentation();

		output.append(tag.getName()).append(": ");
	}

	public void printEnd(Tag tag) {
		depth--;
	}
	
	public void print(Comment comment) {
		depth++;
		output.append("<br/>");
		addIndentation();
		output.append("<i>// ").append(comment.getText()).append("</i>");
		depth--;
	}

	private void addIndentation() {
		for (int i = 0; i < depth; i++) {
			output.append("&nbsp;&nbsp;&nbsp;");
		}
	}

	public void print(Text text) {
		output.append(text.getText());
	}

	public void print(Attribute attribute) {
		output.append("<i>(").append(attribute.getName()).append("=")
				.append(attribute.getValue()).append(")</i> ");
	}

	public String getResultString() {
		return output.toString();
	}

}
